package com.wgw.first.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wgw.first.R;
import com.wgw.first.app.GoodsInfoActivity;
import com.wgw.first.dao.HomeBean;
import com.wgw.first.home.bean.GoodssBean;
import com.wgw.first.utils.Constants;

import java.util.List;

/**
 * @author Admin
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
class NewBookAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<HomeBean.NewbookInfoBean> newbook_info;

    public NewBookAdapter(Context mContext, List<HomeBean.NewbookInfoBean> newbook_info){
        this.mContext = mContext;
        this.newbook_info = newbook_info;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_newbook,null));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.setData(position);
    }

    @Override
    public int getItemCount() {
        return newbook_info.size();
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_newbook_name;
        private TextView tv_newbook_price;
        private ImageView iv_newbook;
        private  LinearLayout ll_root;

        public MyViewHolder(View itemview) {
            super(itemview);
            iv_newbook = (ImageView)itemview.findViewById(R.id.iv_newbook);
            tv_newbook_name = (TextView)itemview.findViewById(R.id.tv_book_name);
            tv_newbook_price = (TextView)itemview.findViewById(R.id.tv_newbook_price);
            ll_root = (LinearLayout) itemview.findViewById(R.id.ll_root);
        }
        public void setData(final int position){
            HomeBean.NewbookInfoBean newbookInfoBean = newbook_info.get(position);
            tv_newbook_name.setText("￥" + newbookInfoBean.getBook_name());
            tv_newbook_price.setText("￥" + newbookInfoBean.getBook_price());
            Glide.with(mContext)
                    .load(Constants.BASE + Constants.IMG + newbookInfoBean.getBook_imgurl())
                    .into(iv_newbook);
            ll_root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "position" + position, Toast.LENGTH_SHORT).show();
                    String price = newbook_info.get(position).getBook_price();
                    String name = newbook_info.get(position).getBook_name();
                    String imgUrl = newbook_info.get(position).getBook_imgurl();
                    String url = newbook_info.get(position).getBook_url();
                    String book_info = newbook_info.get(position).getBook_info();
                    String isbn = newbook_info.get(position).getBook_isbn();

                    GoodssBean.Goods goodsBean = new GoodssBean.Goods(name,price,imgUrl,book_info,url,isbn,Constants.ALEADYLOGIN);
                    Intent intent = new Intent(mContext, GoodsInfoActivity.class);
                    intent.putExtra("goods_bean",goodsBean);
                    mContext.startActivity(intent);
                }
            });

        }
    }
}
